<?php
require_once 'lib/controller.php';
require_once 'view/standard-top.php';


function pageNavLink($totalpages, $pagenumber, $currentpage, $firstname, $surname) {
	if($pagenumber <= $totalpages) {
		if($currentpage != $pagenumber) {
	 		return "<a href=\"professionalsearch.php?surname=".$surname."&firstname=".$firstname."&page=".$pagenumber."\">".$pagenumber."</a>\n\t";	
		} else {
			return "<strong>[ ".$pagenumber." ]</strong>\n\t";
		}
	}
}

function insertSearchPageTopNavigation($surname, $firstname, $totalpages, $currentpage) {

$startpage = $currentpage;
if($currentpage < 3) {
	$startpage = 3;
}

$page1 = $startpage-2;
$page2 = $startpage-1;
$page3 = $startpage;
$page4 = $startpage+1;
$page5 = $startpage+2;

echo "<li class=\"sortby\">";
echo "<a href=\"professionalsearch.php?surname=".$surname."&firstname=".$firstname."&page=1\">&lt;&lt;</a>\n\t";
echo pageNavLink($totalpages, $page1, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page2, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page3, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page4, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page5, $currentpage, $firstname, $surname);
echo "...\n\t";
echo "<a href=\"professionalsearch.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">".$totalpages."</a>\n\t";
echo "<a href=\"professionalsearch.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
echo "</li>";
						
	
}

function insertSearchPageBottomNavigation($surname, $firstname, $totalpages, $currentpage) {

$startpage = $currentpage;
if($currentpage < 3) {
	$startpage = 3;
}

$page1 = $startpage-2;
$page2 = $startpage-1;
$page3 = $startpage;
$page4 = $startpage+1;
$page5 = $startpage+2;

echo "<li class=\"sortby\">";
echo "<a href=\"#topbar\" title=\"Back to Top\" class=\"leftfloat\">Back to Top</a>\n\t";
echo "<a href=\"professionalsearch.php?surname=".$surname."&firstname=".$firstname."&page=1\">&lt;&lt;</a>\n\t";
echo pageNavLink($totalpages, $page1, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page2, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page3, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page4, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page5, $currentpage, $firstname, $surname);
echo "...\n\t";
echo "<a href=\"professionalsearch.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">".$totalpages."</a>\n\t";
echo "<a href=\"professionalsearch.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
echo "</li>";
					
						
	
}

//FILTER THE FORM RESULTS!!!!
$firstname = trim($_GET['firstname']);
$surname = trim($_GET['surname']);
if($firstname == "First Name") {
	$firstname = "";
}

//Gather all possible ProSearch contact details, chuck em in an array
$proSearchContacts = array();
$sql = "SELECT ID, Name, Email, URL, Phone FROM ProSearch";
$result = $db->query($sql);
if ($db->rowCount($result) > 0) {
    while($contact = $db->fetch($result)){
        $contactID = $contact['ID'];
        $proSearchContacts[$contactID] = array("Name"=>$db->unescape($contact['Name']), "Email"=>$db->unescape($contact['Email']), "URL"=>$db->unescape($contact['URL']), "Phone"=>$db->unescape($contact['Phone']));
    }
}

//Make a left join with the 'Contact' field to a table of contacts...
$sql = "select
 	prosearchperson.Fname,
    prosearchperson.Lname,
    prosearchperson.Reason,
    prosearchperson.Contact
from prosearchperson";

//$blah - is a remnant from Ryan, containing the WHERE clause, and also specifying the LIMIT on the query
$blah = "WHERE prosearchperson.Lname Like '".$db->escape($surname)."' AND (prosearchperson.Fname Like '".$db->escape($firstname)."%' OR prosearchperson.Fname Like '% ".$db->escape($firstname)."%') ORDER BY prosearchperson.ID ASC LIMIT ";

$querycount = "select count(*) from prosearchperson WHERE prosearchperson.Lname Like '".$db->escape($surname)."' AND (prosearchperson.Fname Like '".$db->escape($firstname)."%' OR prosearchperson.Fname Like '% ".$db->escape($firstname)."%')";
$resultcount = $db->query($querycount);
$totalCount = 0;
if ($db->rowCount($resultcount) > 0) {
    $Count = $db->fetch($resultcount);
    $totalCount = $Count['count(*)'];
}
$totalpages = 1;
$resultsperpage = 10;
$totalpages = ceil($totalCount/$resultsperpage);
$from = 0;
$to = $resultsperpage;
$currentpage = 1;
//MORE BLAH VAR HERE!
$pagenumber = 0;
if(!empty($_GET['page'])){
    $pagenumber = intval($_GET['page']);
}
if($pagenumber > 0) {
    $currentpage = $pagenumber;
    $to = $pagenumber*$resultsperpage;
    $from = $to-$resultsperpage;
    $blah = $blah.$from.",".$resultsperpage;	
} else {
    $blah = $blah."0,".$resultsperpage;
}
if($pagenumber > 0) {
	if($pagenumber < $totalpages) {
		$pagenumber += 1;
	}	
} else {
	$pagenumber += 2;
}
//BLAH AGAIN
$sql .= ' '.$blah;
// Perform Query
$result = $db->query($sql);
$class1 = "odd";
$class2 = "even";
$class = $class1;
function showfirstname($first) {
	if(strlen($first) > 0) {
		return $first." ";
	}
}
function contactText($contactID){
    global $proSearchContacts;
    $contact = $proSearchContacts[$contactID];
    $urlContact = "<a href='".$contact['URL']."' target='_blank'>".$contact['Name']."</a>";
    if(!empty($contact['Email'])){
        $urlContact = $urlContact." via email <a href='mailto:".$contact['Email']."'>".$contact['Email']."</a>";
    }
    if(!empty($contact['Phone'])){
        $urlContact = $urlContact." by phone on ".$contact['Phone'];
    }
	return "Contact".$urlContact;
}

echo "<ul id=\"searchresults\">";


if($surname != "" && $surname != NULL) {
    echo "<li class=\"header\"><h2>Professional Searching Results for ".$html->text(showfirstname($firstname).$surname)."</h2></li>";
    echo "<li class=\"resultinfo\"><span>(".$totalCount.") listing results &nbsp; (".$resultsperpage.") results per page &nbsp; (".$totalpages.") pages </span>&nbsp</li>";
    if ($db->rowCount($result) > 0) {

        //this <a href=\"#\" title=\"\">Search Tips</a> goes between the above </span> and </li>...
        insertSearchPageTopNavigation($surname, $firstname, $totalpages, $currentpage);

        echo "<li><ul>";
        while($queryResult = $db->fetch($result)){
            echo "<li>";
            echo "<div style='text-align:right;'><div style='float:left;'>".$db->unescape($queryResult['Fname'])." ".$db->unescape($queryResult['Lname'])."</div>".$db->unescape($queryResult['Reason'])."</div>";
            echo "<p>".contactText($queryResult['Contact'])."</p>";
            echo "<div style='font-size:7pt;line-height:9pt;padding-top:3px;'><b>Disclaimer:</b>  Every effort is made to provide current information, but we cannot guarantee that you are the person this organization is searching for.  Good Luck.</div>";
            echo "</li>";
        }

        echo "</ul></li>";

        insertSearchPageBottomNavigation($surname, $firstname, $totalpages, $currentpage);
            
    }else{
        echo "<ul id=\"centerbar\">";
        echo "<li>Dear ".$html->text($firstname)."<br /><br />";
        echo "<p>Sorry but we do not have any professionals searching for the name you provided.</p>
        <p><strong>Have you ever been known by another name?</strong></p>
        <p>You may be remembered by a name you have used previously. Or the professional who is seeking you has the incorrect spelling or only your short (nick) name to search by.</p>
        <p>Please try again using alternate spelling of your name. If you have no luck today please try again another day.</p>
        <p><a title=\"Tell a Friend\" class=\"browsernav_tellfriend\" href=\"mailto:?subject=I personally recommend you visit this site&body=Hi, I'm letting you know that you can see if someone is searching for you by going to http://www.amemorytree.co.nz and entering your name in the Professionals Searching area.
        Legal Professionals use this to make contact with people just like us. Your details aren't given to anyone, it’s FREE and you don't need to register and it 10 seconds. Good Luck!
        \">Tell a Friend</a> about <a href=\"http://www.amemorytree.co.nz\">www.amemorytree.co.nz</a> today.</p></li>";
        echo "</ul>";
    }
} else {
    echo "<li><p>Sorry we have no matches for the names you supplied.</p></li>";
}
echo "</ul>";

require_once 'view/standard-bottom.php';
?>





